﻿using DoNetDrive.Common.Extensions;
using Face8280WebSocketServer.DB.Record;
using Face8280WebSocketServer.DTO.FaceRecard;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Service
{
    public class FaceRecordService : BaseService, IFaceRecordService
    {
        IFacePeopleService _FacePeopleService;
        public FaceRecordService(DbContext context, IFacePeopleService facePeople) : base(context)
        {
            _FacePeopleService = facePeople;
        }

        public void AddRecord(FaceRecordInfo recordInfo)
        {
            var people = _FacePeopleService.GetPeopleByCode(recordInfo.Code);
            if (people != null)
            {
                recordInfo.Name = people.Name;
            }
            if (Set<FaceRecordInfo>().Any(a => a.DeviceSn.Equals(recordInfo.DeviceSn) &&
             a.Date == recordInfo.Date && a.Code == recordInfo.Code))
            {
                return;
            }
            if (recordInfo.Photo.Equals("1") && recordInfo.PhotoData != null)
            {
                var md5 = MD5Utility.GetFileMD5(recordInfo.PhotoData);
                var fileName = recordInfo.Date.ToFileTimeUtc() + $"{md5}.jpg";
                recordInfo.Photo = FileIOUtility.WriteRecordImage(fileName, recordInfo.DeviceSn, recordInfo.PhotoData);
            }
            else
            {
                recordInfo.Photo = "";
            }
            Insert(recordInfo);
        }
        /// <summary>
        /// 获取记录列表
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public JsonResultModel GetCardRecordList(QueryCardRecordParameter parameter)
        {
            var recordList = Set<FaceRecordInfo>().AsQueryable();
            if (parameter.Name != null)
            {
                recordList = recordList.Where(a => a.Name.Contains(parameter.Name));
            }
            if (parameter.DeviceSn != null)
            {
                recordList = recordList.Where(a => a.DeviceSn.Contains(parameter.DeviceSn));
            }
            recordList = recordList.Where(a => a.Date >= parameter.StartTime && a.Date <= parameter.EndTime);
            int count = recordList.Count();
            recordList.OrderByDescending(a => a.Id).
                Skip((parameter.pageIndex - 1) * parameter.pageSize).Take(parameter.pageSize);
            PageResult<FaceRecordInfo> result = new();
            result.TotalCount = count;
            result.PageIndex = parameter.pageIndex;
            result.PageSize = parameter.pageSize;
            result.DataList = recordList.ToList();
            return new JsonResultModel(result);

        }

        public void ClearReport()
        {
            Excute("DELETE FROM FaceRecordNew");
        }
    }
}
